﻿using System;
using System.Collections.Generic;
using System.Data;
using MedicService.Domain.Directory;
using MedicService.Server.Factory.Directory.Simple;
using MedicService.Server.Factory.Generic;

namespace MedicService.Server.Factory.Directory {
    public class InstitutionFactory : DirectoryFactory<Institution> {
        public InstitutionFactory() : base("Institution") { }

        protected override void FillEntity(Institution entity, DataRow myDataRow) {
            entity.Id = (int)myDataRow["id"];
            entity.Title = (string) myDataRow["title"];
            entity.InstitutionAddress = myDataRow["InstitutionAddress"].ToString();
            entity.InstitutionPhone = myDataRow["InstitutionPhone"].ToString();
            entity.InstitutionType = new InstitutionTypeFactory().Load(myDataRow["InstitutionTypeId"]);
            entity.Locality = new LocalityFactory().Load(myDataRow["LocalityId"]);
            entity.Ownership = new OwnershipFactory().Load(myDataRow["OwnershiId"]);
            var rmr = myDataRow["RMR"] is Guid ? (Guid) myDataRow["RMR"] : Guid.Empty;
            entity.Rmr = rmr;
        }

        public List<Institution> GetList(Locality locality) {
            var entities = new List<Institution>();
            var dataTable = DatabaseConnector.GetData(TableName, OrderBy, "LocalityId=" + locality.Id);
            foreach (DataRow myDataRow in dataTable.Rows) {
                var entity = CreateEntity();
                FillEntity(entity, myDataRow);
                entities.Add(entity);
            }
            return entities;
        }

        protected override Institution CreateEntity() {
            return new Institution();
        }

        protected override string GetInsertQuery(Institution entity) {
            string q = "INSERT INTO [Institution]"
                       + "           ([title]"
                       + "           ,[InstitutionTypeId]"
                       + "           ,[OwnershiId]"
                       + "           ,[InstitutionAddress]"
                       + "           ,[InstitutionPhone]"
                       + "           ,[RMR]"
                       + "           ,[LocalityId])"
                       + "     VALUES"
                       + "           (" + Nullable(entity.Title)
                       + "           ," + Nullable(entity.InstitutionType)
                       + "           ," + Nullable(entity.Ownership)
                       + "           ," + Nullable(entity.InstitutionAddress)
                       + "           ," + Nullable(entity.InstitutionPhone)
                       + "           ," + Nullable(entity.Rmr)
                       + "           ," + Nullable(entity.Locality) + ");"
                       + " SELECT SCOPE_IDENTITY()";
            return q;
        }

        protected override string GetUpdateQuery(Institution entity) {
            string q = "UPDATE [Institution]"
                     + "   SET [title] = " + Nullable(entity.Title)
                     + "      ,[InstitutionTypeId] = " + Nullable(entity.InstitutionType)
                     + "      ,[OwnershiId] = " + Nullable(entity.Ownership)
                     + "      ,[InstitutionAddress] = " + Nullable(entity.InstitutionAddress)
                     + "      ,[InstitutionPhone] = " + Nullable(entity.InstitutionPhone)
                     + "      ,[LocalityId] = " + Nullable(entity.Locality)
                     + "      ,[RMR] = " + Nullable(entity.Rmr)
                     + " WHERE id=" + entity.Id;
            return q;
        }
    }
}